﻿
using DevExpress.Mvvm;
using DevExpress.Office.Utils;
using DevExpress.XtraPrinting.Native;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Data;
using  DXApplications.Models;
namespace DXApplications.ViewModels
{
    public class StudyStatsViewModel : ViewModelBase
    {
        // 修复 CS8652: 移除不受支持的“field”修饰符（如有），并使用标准属性语法。
        // 你的代码中并未直接出现“field”修饰符，但 Observable<DateTime> SelectedTime 的 set 语法不正确。
        // 应将 set 语法改为标准属性实现，并在 setter 中调用 RefreshAllAnalyses。

        public Observable<DateTime> SelectedTime
        {
            get => _selectedTime;
            set
            {
                if (_selectedTime != value)
                {
                    _selectedTime = value;
                    RefreshAllAnalyses(); // 每次选中时间改变时刷新所有分析数据
                    RaisePropertyChanged(nameof(SelectedTime));
                }
            }
        }
        private Observable<DateTime> _selectedTime;

        public ObservableCollection<StudyRecordModel>AllRecords  { get; set; }

        // 替换属性类型和相关赋值，修复 CS0266

        public ObservableCollection<StudyRecordModel> DailyAnalysisData { get; private set; } // 日分析数据（饼图）
        public ObservableCollection<StudyRecordModel> WeeklyAnalysisData { get; private set; } // 周分析数据（堆叠柱状图）
        public ObservableCollection<StudyRecordModel> YearlyAnalysisData { get; private set; } // 年分析数据（堆叠柱状图）

        public StudyStatsViewModel()
        {
            // 初始化逻辑
            Initialize();
        }
        private void Initialize()
        {
            // 这里可以添加初始化代码，例如加载数据或设置默认值
            // 例如：LoadStudyStatistics();
        }
        // 示例方法：加载学习统计数据
        private void LoadStudyStatistics()
        {
            // 实现加载学习统计数据的逻辑
            // 例如从数据库或API获取数据并更新视图模型属性
        }
        private void RefreshAllAnalyses()
        {
            RefreshDailyAnalysis();   // 日分析（饼图）
            RefreshWeeklyAnalysis();  // 周分析（堆叠柱状图）
            RefreshYearlyAnalysis();  // 年分析（堆叠柱状图）
        }

        // ---------- 日分析：选中日期的各科目时长占比（饼图） ----------
        private void RefreshDailyAnalysis()
        {
            List<StudyRecordModel> AllStudyRecords = AllRecords.ToList();
            var dailyData = from record in AllStudyRecords
                            where record.Date.Date == SelectedTime.Value.Date
                            group record by record.Subject into g
                            select new StudyRecordModel
                            {
                                Subject = g.Key,
                                Duration = g.Sum(r => r.Duration),
                                Date = SelectedTime.Value.Date
                            };
            DailyAnalysisData.Clear();
            foreach (var item in dailyData)
            {
                DailyAnalysisData.Add(item);
            }
               
        }

        // ---------- 周分析：选中日期所在周，每天的科目时长堆叠（柱状图） ----------
        private void RefreshWeeklyAnalysis()
        {
            List<StudyRecordModel> AllStudyRecords = AllRecords.ToList();
            DateTime startOfWeek = SelectedTime.Value.AddDays(-(int)SelectedTime.Value.DayOfWeek + 1);
            DateTime endOfWeek = startOfWeek.AddDays(6);

            var weeklyData = from record in AllStudyRecords
                             where record.Date >= startOfWeek && record.Date <= endOfWeek
                             group record by new { record.Date, record.Subject } into g
                             select new StudyRecordModel
                             {
                                 Date = g.Key.Date,
                                 Subject = g.Key.Subject,
                                 Duration = g.Sum(r => r.Duration)
                             };

            WeeklyAnalysisData.Clear();
            foreach (var item in weeklyData)
            {
                WeeklyAnalysisData.Add(item);
            }
        }

        // ---------- 年分析：选中日期所在年，每月的科目时长堆叠（柱状图） ----------
        private void RefreshYearlyAnalysis()
        {
            List<StudyRecordModel> AllStudyRecords = AllRecords.ToList();
            var yearlyData = from record in AllStudyRecords
                             where record.Date.Year == SelectedTime.Value.Year
                             group record by new { Month = record.Date.Month, record.Subject } into g
                             select new StudyRecordModel
                             {
                                 Date = new DateTime(SelectedTime.Value.Year, g.Key.Month, 1),
                                 Subject = g.Key.Subject,
                                 Duration = g.Sum(r => r.Duration)
                             };

            YearlyAnalysisData.Clear();
            foreach (var item in yearlyData)
            {
                YearlyAnalysisData.Add(item);
            }
        }

    }
}
